package com.thingple.usb;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.Log;

public class BatteryMonitor extends BroadcastReceiver {
    private static final String TAG = BatteryMonitor.class.getName();

    public interface LowPowerListener {
        void onLowPower();
    }

    public interface BatteryChangeListener{
        void onChange(int percent);
    }

    public interface ChargeListener {
        void onChargeStatusChange(boolean charging);
    }

    private Context ctx;

    private LowPowerListener lowPowerListener;
    private BatteryChangeListener batteryChangeListener;
    private ChargeListener chargeListener;

    public void setLowPowerListener(LowPowerListener listener) {
        lowPowerListener = listener;
    }
    public void setBatteryChangeListener(BatteryChangeListener listener) {
        batteryChangeListener = listener;
    }
    public void setChargeListener(ChargeListener listener) {
        chargeListener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
            int current = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int total = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
            int percent = (int)(current * 100 / (float)total);
            Log.d(TAG, "battery: " + percent);
            if (batteryChangeListener != null) {
                batteryChangeListener.onChange(percent);
            }
        } else if (Intent.ACTION_BATTERY_LOW.equals(action)) {
            Log.d(TAG, "battery low");
            if (lowPowerListener != null) {
                lowPowerListener.onLowPower();
            }
        } else if (Intent.ACTION_BATTERY_OKAY.equals(action)) {
            Log.d(TAG, "battery ok");
        } else if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
            Log.d(TAG, "Charging");
            if (chargeListener != null) {
                chargeListener.onChargeStatusChange(true);
            }
        } else if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
            Log.d(TAG, "NoneCharge");
            if (chargeListener != null) {
                chargeListener.onChargeStatusChange(false);
            }
        } else {
            Log.d(TAG, "unknown battery action");
        }
    }

    public void start(Context ctx) {
        this.ctx = ctx;
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(Intent.ACTION_BATTERY_LOW);
        filter.addAction(Intent.ACTION_BATTERY_OKAY);

        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        ctx.registerReceiver(this, filter);
    }

    public void onDestroy() {
        if (ctx != null) {
            ctx.unregisterReceiver(this);
        }
    }
}